use "${datadir}\HTB_main", replace

keep if htb_dummy==1 

gen 		loan_val_counter 	= (LTV + 20) / 100 * property_val
replace 	loan_val_counter 	= (LTV + 40) / 100 * property_val if htb_london

gen 		LTV_round_counter = LTV_round
replace 	LTV_round_counter = LTV_round_counter + 20
replace 	LTV_round_counter = LTV_round_counter + 20 if htb_london

gen 		band_id_counter = 1 if LTV_round_counter<=60
replace 	band_id_counter = 2 if LTV_round_counter>60 & LTV_round_counter<=70
replace 	band_id_counter = 3 if LTV_round_counter>70 & LTV_round_counter<=75
replace 	band_id_counter = 4 if LTV_round_counter>75 & LTV_round_counter<=80
replace 	band_id_counter = 5 if LTV_round_counter>80 & LTV_round_counter<=85
replace 	band_id_counter = 6 if LTV_round_counter>85 & LTV_round_counter<=90
replace 	band_id_counter = 7 if LTV_round_counter>90 
label 		define band_id_counterl 1 "40<ltv<=60" 2 "60<ltv<=70" 3 "70<ltv<=75" 4 "75<ltv<=80" 5 "80<ltv<=85" 6 "85<ltv<=90" 7 "90<ltv<=95"
label 		values band_id_counter band_id_counterl
tab 		band_id_counter

drop _merge
merge m:1 advtype dealtype lender_id band_id_counter yearm_date using /// 
	"${datadir}\counterfactual_rate", keep(1 3) 

gen diff = interest_counter - interest	

replace interest_counter = interest + 0.80 if missing(interest_counter)

gen 		interest_counter00 	= interest_counter/100

gen 	payment2 	= (loan_val_counter) * ( (interest_counter00)/12 * (1+(interest_counter00)/12)^(mortgage_term * 12) ) / ((1+(interest_counter00)/12)^(mortgage_term * 12) - 1)
gen 	PTInet2 	= payment2 / (net_income/12) * 100

gen 	payment3 	= (loan_val_counter) * ( (interest00)/12 * (1+(interest00)/12)^(mortgage_term * 12) ) / ((1+(interest00)/12)^(mortgage_term * 12) - 1)
gen 	PTInet3 	= payment3 / (net_income/12) * 100


gen tmp = _N

gen PTInet_roundup50pct	 				 				= round(PTInet)
bys PTInet_roundup50pct htb_dummy: gen tmp1 			= _N
replace tmp1 											= 100 * tmp1/tmp
bys PTInet_roundup50pct htb_dummy: gen fo_tmp1 			= (_n==1)

forval i = 2(1)3 { 
	gen PTInet`i'_roundup50pct	 				 		= round(PTInet`i')
	bys PTInet`i'_roundup50pct htb_dummy: gen tmp`i' 	= _N
	replace tmp`i' 										= 100 * tmp`i'/tmp
	bys PTInet`i'_roundup50pct htb_dummy: gen fo_tmp`i'	= (_n==1)
}
*

	twoway ///
			(connected tmp1 PTInet_roundup50pct 	if fo_tmp1==1 & htb_dummy==1 & PTInet_roundup50pct<70, msize(medsmall) sort ) /// 
			(connected tmp2 PTInet2_roundup50pct 	if fo_tmp2==1 & htb_dummy==1 & PTInet2_roundup50pct<70, msize(medsmall) sort msymbol(D))  ///
			(connected tmp3 PTInet3_roundup50pct 	if fo_tmp3==1 & htb_dummy==1 & PTInet3_roundup50pct<70, msize(medsmall) sort msymbol(X)) , ///
			xline(10 20 30 40 50 60, lwidth(vthin) lcolor(black) lpattern(dash)) ///	
			xtitle("Payment-To-Net Income (%)") 
			xscale(range(0(1)70)) xlabel(0(5)70) xmtick(0(1)70)  ///
			yscale(range(0(1)4)) ylabel(0(1)6) ymtick(0(1)6)  ///
			legend(order(1 "Equity loan" 2 "Standard 95%" 3 "Mortgage guarantee" ) pos(1) ring(0) col(1) size(small))	///
			graphregion(color(white)) bgcolor(white) 
graph export "${figuredir}\kdensity_HTB_VS_MG_Joao.pdf", as(pdf) replace